/*
 * Copyright 2008 - 2010 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.dm.impl;

import com.semagia.atomico.dm.IResource;

/**
 * Immutable {@link IResource} implementation.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 */
public class Resource implements IResource {

    private final String _iri;
    private final int _kind;

    public Resource(final String iri) {
        this(iri, IResource.UNKNOWN);
    }

    public Resource(final String iri, final int kind) {
        if (iri == null) {
            throw new IllegalArgumentException("The IRI must not be null");
        }
        if (kind != IResource.UNKNOWN && kind != IResource.ITEM_IDENTIFIER 
                && kind != IResource.SUBJECT_IDENTIFIER && kind != IResource.SUBJECT_LOCATOR) {
            throw new IllegalArgumentException("The kind of resource is invalid: " + kind);
        }
        _iri = iri;
        _kind = kind;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IResource#getIRI()
     */
    @Override
    public String getIRI() {
        return _iri;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IResource#getKind()
     */
    @Override
    public int getKind() {
        return _kind;
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof IResource)) {
            return false;
        }
        return _kind == ((IResource) other).getKind() && _iri.equals(((IResource) other).getIRI());
    }

    @Override
    public int hashCode() {
        return _iri.hashCode() + _kind;
    }

}
